/************************************************************************
Author:			Trey Turley, Thomas Lee, Marc Willis

Filename:		DriverDraw.cpp

Classname:		Plotter

Description:	The plotter class allows us to plot characters
				to the screen. It uses a coordinate system to
				determine where the character will be placed.

Date Modified:	8/7/10
				- Converted from test driver to working game
				8/3/10
				- File created
*************************************************************************/


	//This currently prints a basic title followed
	//by the map and some basic directions.
	//It also has a basic loop setup that can be used to draw two different
	//Trails(Players) on the map. There is no bounds/collision detection in the test.
	//Need to look into pairing the plotter class with the players/AI.

const int SLEEPTIME=50;

const int TITLEX=6;
const int TITLEY=3;

#include <iostream>
using namespace std;
#include "HPlayer.h"
//#include "keyboard.h"

void pause()
{
	while(!(KEYDOWN(VK_RETURN)));
	cin.clear();
}

int main()
{

	int numPlayers=0;
	bool startGame=false, openMenu=false;

//resizing console window
	HWND console = GetConsoleWindow();
	RECT r;
	GetWindowRect(console, &r); //stores the console's current dimensions
	MoveWindow(console, r.left, r.top, 800, 600, TRUE);

//Main Title
	Plotter Title;
	Title.setColor(white);
	Title.move(TITLEX+2,TITLEY+2);
	Title.setColor(blue);
	cout<<"@@@@@";
	Title.setColor(red);
	cout<<" @@@@ ";
	Title.setColor(green);
	cout<<"  @@@ ";
	Title.setColor(yellow);
	cout<<" @   @";
	Title.move(TITLEX+2,TITLEY+3);
	Title.setColor(blue);
	cout<<"  @   ";
	Title.setColor(red);
	cout<<"@   @ ";
	Title.setColor(green);
	cout<<"@   @ ";
	Title.setColor(yellow);
	cout<<"@@  @";
	Title.move(TITLEX+2,TITLEY+4);
	Title.setColor(blue);
	cout<<"  @   ";
	Title.setColor(red);
	cout<<"@@@@  ";
	Title.setColor(green);
	cout<<"@   @ ";
	Title.setColor(yellow);
	cout<<"@ @ @ ";
	Title.move(TITLEX+2,TITLEY+5);
	Title.setColor(blue);
	cout<<"  @   ";
	Title.setColor(red);
	cout<<"@   @ ";
	Title.setColor(green);
	cout<<"@   @ ";
	Title.setColor(yellow);
	cout<<"@  @@ ";
	Title.move(TITLEX+2,TITLEY+6);
	Title.setColor(blue);
	cout<<"  @   ";
	Title.setColor(red);
	cout<<"@   @ ";
	Title.setColor(green);
	cout<<"@   @ ";
	Title.setColor(yellow);
	cout<<"@   @ ";
	Title.move(TITLEX+2,TITLEY+7);
	Title.setColor(blue);
	cout<<"  @   ";
	Title.setColor(red);
	cout<<"@   @ ";
	Title.setColor(green);
	cout<<" @@@  ";
	Title.setColor(yellow);
	cout<<"@   @ ";
	Title.setColor(white);
	Title.plot(TITLEX,TITLEY,201);
	Title.plot(TITLEX,TITLEY+9,200);
	Title.plot(TITLEX+26,TITLEY,187);
	Title.plot(TITLEX+26,TITLEY+9,188);
	for(int a=1;a<26;a++)
	{
		Title.plot(TITLEX+a,TITLEY,205);
		Title.plot(TITLEX+a,TITLEY+9,205);
	}

	for(int b=1;b<9;b++)
	{
		Title.plot(TITLEX,TITLEY+b,186);
		Title.plot(TITLEX+26,TITLEY+b,186);
	}
	Title.move(40,6);
	cout<<"By:";
	Title.move(40,7);
	cout<<"Thomas Lee";
	Title.move(40,8);
	cout<<"Marc Willis";
	Title.move(40,9);
	cout<<"Trey Turley";

	//cout<<"\n\nPress enter to continue to Main Menu\n";
	
	pause();

	bool running=true;
	while(running){
		Title.clear();	

		cout<<"Player colors are:\n";
		Title.setColor(blue);
		cout<<"Player 1: Blue\n";
		Title.setColor(red);
		cout<<"Player 2: Red\n";
		Title.setColor(green);
		cout<<"Player 3: Green\n";
		Title.setColor(yellow);
		cout<<"Player 4: Yellow\n\n";
		Title.setColor(white);

		cout<<"Enter the number of players and press enter to play!\n\n";
		cin>>numPlayers;

		

		
		while(!startGame)
		{
			while(numPlayers<1||numPlayers>4)
			{
				cout<<"Incorrect number of players entered...\nTry again: ";
				cin>>numPlayers;
			}
			if(KEYDOWN(VK_RETURN))
			{
				startGame=true;
			}
		}	

		Title.clear();
		cout<<"Player Controls:\n\n";
		Title.setColor(blue);
		switch (numPlayers)
		{
		case 4:	cout<<"Player 1 will use WASD"<<endl;
				Title.setColor(red);
				cout<<"Player 2 will use the arrow keys\n";
				Title.setColor(green);
				cout<<"Player 3 will use IJKL \n";
				Title.setColor(yellow);
				cout<<"Player 4 will use 8456 on the numPad \n";
				Title.setColor(white);
				break;
		case 3:	cout<<"Player 1 will use WASD"<<endl;
				Title.setColor(red);
				cout<<"Player 2 will use the arrow keys\n";
				Title.setColor(green);
				cout<<"Player 3 will use IJKL \n";
				Title.setColor(white);		
				break;
		case 2:	cout<<"Player 1 will use WASD"<<endl;
				Title.setColor(red);
				cout<<"Player 2 will use the arrow keys\n";
				Title.setColor(white);				
				break;
		case 1:	cout<<"Player 1 will use WASD \n";
				Title.setColor(white);
				break;
		};



		cout<<"\n\nThe Game Begins in: ";

		for(int t=5;t>0;t--)
		{
			cout<<t<<" ";
			Sleep(1000);
		}

		cout<<"GO!";

		Sleep(250);
		
	//clear it!
		Title.clear();

	//Fill in the border of the map
		Plotter map;
		map.setColor(white);

		map.plot(0,0,201);
		map.plot(0,40,200);
		map.plot(79,0,187);
		map.plot(79,40,188);
		for(int a=1;a<79;a++)
		{
			map.plot(a,0,205);
			map.plot(a,40,205);
		}

		for(int b=1;b<40;b++)
		{
			map.plot(0,b,186);
			map.plot(79,b,186);
		}



	//Move cursor to bottom of map.
		map.move(0,41);

	//Print some directions under the map
		Plotter Directions;
		Directions.setColor(white);
		cout<<"Directions for Tron: The goal is to trap your opponent."<<endl;
		cout<<"Try not to run into the walls or your opponent's trail."<<endl;


	//	begin game preperations
		GameMap game(80,41,map);
		HPlayer unPlayer(0,0,0,0,game);
		unPlayer.setAlive(false);
		Player *p1,*p2,*p3,*p4;
		p1=new HPlayer(1,1,DIR_RIGHT,blue,game,CTRL_WASD);
		p2=new HPlayer(78,39,DIR_LEFT,red,game,CTRL_ARROWS);
		if(numPlayers>=3)
		{
			p3=new HPlayer(78,1,DIR_DOWN,green,game,CTRL_IJKL);
		}else{
			p3=&unPlayer;
		}
		if(numPlayers==4)
		{
			p4=new HPlayer(1,39,DIR_UP,yellow,game,CTRL_NUM);
		}else{
			p4=&unPlayer;
		}
		bool quit=false;
	//Press ESC to quit out of the loop and end the game

		while(!quit)
		{
			try
			{
				if(p1->isAlive()){
					p1->advance();
				}
				if(p2->isAlive()){
					p2->advance();
				}
				if(p3->isAlive()){
					p3->advance();
				}
				if(p4->isAlive()){
					p4->advance();
				}
				Sleep(SLEEPTIME);
			}
			catch(GMException v)
			{
				map.move(0,43);
				v.printEx(cout);
				cout<<"\nPlayer1 x:"<<p1->getX()<<"\tPlayer1 y:"<<p1->getY();
				cout<<"\nPlayer2 x:"<<p2->getX()<<"\tPlayer2 y:"<<p2->getY();
				quit=true;
			}
			//checks to see if the quit button is pressed or if one or fewer players is alive
			if(KEYDOWN(VK_ESCAPE)||(p1->isAlive()+p2->isAlive()+p3->isAlive()+p4->isAlive()<=1))
			{
				quit=true;
			}
			
		}
		map.move(0,44);
		if(p1->isAlive()+p2->isAlive()+p3->isAlive()+p4->isAlive()>1)
		{
			map.setColor(white);
			cout<<"You quit!\n";
		}else{
			if(p1->isAlive()+p2->isAlive()+p3->isAlive()+p4->isAlive()==0)
			{
				map.setColor(white);
				cout<<"Draw!\n";
			}
			if(p1->isAlive())
			{
				map.setColor(p1->getColor());
				cout<<"Player 1 wins!\n";
			}
			if(p2->isAlive())
			{
				map.setColor(p2->getColor());
				cout<<"Player 2 wins!\n";
			}
			if(p3->isAlive())
			{
				map.setColor(p3->getColor());
				cout<<"Player 3 wins!\n";
			}
			if(p4->isAlive())
			{
				map.setColor(p4->getColor());
				cout<<"Player 4 wins!\n";
			}
		}
		cout<<"Press enter to continue.";
		pause();
		delete p1;
		delete p2;
		if(p3!= &unPlayer)
		{
			delete p3;
		}
		if(p4!= &unPlayer)
		{
			delete p4;
		}
		map.clear();
		map.move(0,0);
		map.setColor(white);
		cout<<"Play again(Y/N)?";
		char res;
		cin>>res;
		if(toupper(res)!='Y')
		{
			running=false;
		}
	}
	return 0;
}
